/**
 * 获得搜索参数
 * @param params
 * @returns {Object}
 */
function getSearchParams(params) {
    var searchParams = new Object();
    if (params != undefined) {
        searchParams = params;
    }
    $('.SearchForm').each(function() {
        var param = $(this).val().trim();
        var val = $("input:checkbox[name='sex']:selected").val();
        if (val == 1) {
            searchParams['sex'] = 1;
        } else if (val == 2) {
            searchParams['sex'] = 2;
        }
        if (param == undefined)
            param = '';
        searchParams[$(this).attr('name')] = param;
    });
    return searchParams;
}
//清空搜索条件
$("#Reset").click(function() {
    $('.SearchForm').val('');
    $("input:radio[name='sex']").removeAttr('checked');
});

/**
 * 页面自加载
 */
$(function() {
    // $('#student_createtime').datepicker({
    //     format: "yyyy-mm-dd",
    //     todayBtn: "linked",
    //     autoclose: true,
    //     todayHighlight: true,
    //     clearBtn: true
    // });

    var cellwidth = ($(".box-content.table-responsive").width() - 55) / 11;
    var psval = $('#datatable_studentinfo').attr('data-size');
    if (psval == undefined || psval == "") {
        psval = 10;
    }
    /**
     * easyui 生成表格数据
     */
    $('#datatable_studentinfo').datagrid({
        singleSelect: true, //允许选择多行
        striped: true,
        idField: 'id',
        remoteSort: false,
        collapsible: true,
        fit: false,
        url: 'admin/Lunbo/getDatas', //数据源路径
        loadMsg: '请等待数据载入....',
        pagination: true,
        rownumbers: true,
        fitColumns: true,
        pageSize: psval,
        pageList: [psval, psval * 2, psval * 3, psval * 4, psval * 5],
        columns: [[{
            field: 'lb_name',
            title: '名称',
            align: 'center',
            width: cellwidth,
            sortable: true
        },{
            field: 'lb_image',
            title: '轮播图',
            align: 'center',
            width: cellwidth,
            sortable: true,
            formatter: function (value) {
                if (value !== "") {
                    return "<img src='"+value+"' style='width:180px;height:100px;margin:auto'>";
                }
            }
        },{
            field: 'lb_time',
            title: '时间',
            align: 'center',
            width: cellwidth,
            sortable: true
        }, {
            field: 'lb_status',
            title: '状态',
            align: 'center',
            width: cellwidth,
            sortable: true,
            formatter: function (value) {
                if (value == 1) {
                    return "<span style='color:green;font-weight: bold'>启用</span>";
                }
                if (value == 2) {
                    return "<span style='color:red;font-weight: bold'>禁用</span>";
                }
            }
        }, {
            field: 'lb_id',
            title: '操作',
            align: 'center',
            width: cellwidth,
            sortable: true,
            formatter:function(value,row,index){
                var html ='';
                if(row.lb_status == 1){
                    html ="<a onclick='TyInfo(this);' style='color:#F17A02;margin-left:5px;font-weight: bold' name='"+row.lb_id+"' href='JavaScript:;'>禁用</a>&nbsp;&nbsp;&nbsp;";
                }else{
                    html ="<a onclick='QyInfo(this);' style='margin-left:5px;font-weight: bold' name='"+row.lb_id+"' href='JavaScript:;'>启用</a>&nbsp;&nbsp;&nbsp;";
                }
                return html;
            }
        }]],
        onBeforeLoad: function (param) {
            param = getSearchParams(param);
        },
    });
    /*搜索*/
    $('#Search').click(function () {
        $('#datatable_studentinfo').datagrid('reload');
    })

    /**
     * 编辑
     */
    $("#edit").click(function() {
        var row = $('#datatable_studentinfo').datagrid('getSelections');
        if (row.length < 1) {
            $.TeachDialog({
                content: '请选择一行数据进行修改！',
            });
            return;
        }
        if (row.length > 1) {
            $.TeachDialog({
                content: '只能选择一行进行修改！',
            });
            return;
        }

        //获得修改框内容
        var content = "";
        $.ajax({
            url: 'admin/Lunbo/editLunbo',
            data: "id=" + row[0].lb_id,
            type: 'POST',
            dataType: 'HTML', //返回的数据类型
            success: function(updatemodalhtml) {
                content = updatemodalhtml;
                //弹出修改框
                $.TeachDialog({
                    modalId: null,
                    animation: null,
                    title: '修改轮播图',
                    content: content,
                    showCloseButton: true,
                    showCloseButtonName: '关闭',
                    CloseButtonAddFunc: function() {
                    },
                    otherButtons: ['修改'],
                    otherButtonStyles: [],
                    bootstrapModalOption: {
                        backdrop: 'static'
                    },
                    largeSize: true,
                    smallSize: false,
                    dialogShow: function() {
                    },
                    dialogShown: function() {
                    },
                    dialogHide: function() {
                    },
                    dialogHidden: function() {
                    },
                    clickButton: function(sender, modal, index) {
                        var con = editor.txt.html();
                        $('#con').val(con);
                        var formData = new FormData($( "#editForm" )[0]);
                        $.ajax({
                            url: 'admin/Lunbo/edit',
                            data: formData,
                            type: 'POST',
                            async: false,//1，这四个要写上，否则点击没反应
                            cache: false,//2
                            contentType: false,//3
                            processData: false,//4
                            dataType: 'JSON',
                            success: function(returnData) {
                                if (returnData.status == 0) {
                                    layer.msg(returnData.msg,{icon:5,time:2000});
                                } else {
                                    modal.modal('hide');
                                    layer.msg(returnData.msg,{icon:6,time:2000});
                                    $('#datatable_studentinfo').datagrid('reload');
                                }
                            },
                            error: function() {
                                // $.TeachDialog({
                                //     content: '修改失败',
                                // });
                                layer.msg("修改失败",{icon:5,time:2000});
                            },
                        });
                    },
                });
            },
            error: function() {
                // $.TeachDialog({
                //     content: '获取数据失败，无法进行修改',
                // });
                layer.msg("获取数据失败，无法进行修改",{icon:5,time:2000});
                return;
            }
        });
    });

    /**
     * 添加
     */
    $("#add").click(function() {
        var row = $('#datatable_studentinfo').datagrid('getSelections');
        //获得修改框内容
        var content = "";
        $.ajax({
            url: 'admin/Lunbo/addLunbo',
            data: "",
            type: 'POST',
            dataType: 'HTML', //返回的数据类型
            success: function(updatemodalhtml) {
                content = updatemodalhtml;
                //弹出修改框
                $.TeachDialog({
                    modalId: null,
                    animation: null,
                    title: '添加轮播图',
                    content: content,
                    showCloseButton: true,
                    showCloseButtonName: '关闭',
                    CloseButtonAddFunc: function() {
                    },
                    otherButtons: ['添加'],
                    otherButtonStyles: [],
                    bootstrapModalOption: {
                        backdrop: 'static'
                    },
                    largeSize: true,
                    smallSize: false,
                    dialogShow: function() {
                    },
                    dialogShown: function() {
                    },
                    dialogHide: function() {
                    },
                    dialogHidden: function() {
                    },
                    clickButton: function(sender, modal, index) {
                        var con = editor.txt.html();
                        $('#con').val(con);
                        var formData = new FormData($( "#addForm" )[0]);
                        $.ajax({
                            url: 'admin/Lunbo/add',
                            data: formData,
                            type: 'POST',
                            async: false,//1，这四个要写上，否则点击没反应
                            cache: false,//2
                            contentType: false,//3
                            processData: false,//4
                            dataType: 'JSON',
                            success: function(returnData) {
                                if (returnData.status == 0) {
                                    layer.msg(returnData.msg,{icon:5,time:2000});
                                } else {
                                    modal.modal('hide');
                                    layer.msg(returnData.msg,{icon:6,time:2000});
                                    $('#datatable_studentinfo').datagrid('reload');
                                }
                            },
                            error: function() {
                                // $.TeachDialog({
                                //     content: '添加失败',
                                // });
                                layer.msg("添加失败",{icon:5,time:2000});
                            },
                        });
                    },
                });
            },
            error: function() {
                // $.TeachDialog({
                //     content: '获取数据失败，无法进行添加',
                // });
                layer.msg("获取数据失败，无法进行添加",{icon:5,time:2000});
                return;
            }
        });
    });


    /**
     * 删除
     */
    $('#del').click(function() {
        var row = $('#datatable_studentinfo').datagrid('getSelections');
        if (row.length < 1) {
            $.TeachDialog({
                content: '请选择一行数据进行修改！',
            });
            return;
        }
        if (row.length > 1) {
            $.TeachDialog({
                content: '只能选择一行进行修改！',
            });
            return;
        }
        $.TeachDialog({
            content: "确认删除此轮播图？",
            showCloseButton: true,
            showCloseButtonName: '取消',
            otherButtons: ['确认'],
            otherButtonStyles: ['btn-danger'],
            clickButton: function(sender, modal, index) {
                modal.modal('hide');
                $.ajax({
                    url: 'Admin/Lunbo/deleteLunbo', //form action
                    dataType: 'JSON', //返回体类型
                    type: 'POST', // form type
                    data: "id=" + row[0].lb_id,
                    success: function(data) {
                        if (data.status == 1) {
                            layer.msg(data.msg,{icon:6,time:2000});
                            $('#datatable_studentinfo').datagrid('reload');
                            $('#datatable_studentinfo').datagrid('uncheckAll');
                        } else {
                            layer.msg(data.msg,{icon:5,time:2000});
                        }
                    },
                    error: function() {
                        layer.msg("删除失败!",{icon:5,time:2000});
                    }
                });
            }
        })
    });

})
